package com.dyz.LeetCodeforSwordFingerOffer;

import java.util.ArrayList;
import java.util.HashMap;

public class FirstUniqChar50 {
    public static void main(String[] args) {
        char ch = new FirstUniqChar50().firstUniqChar("leetcode");
        System.out.println(ch);
    }
    public char firstUniqChar(String s) {
        //第一次， 出现一次
        //处理特殊情况
        if(s.length()<=0){return ' ';}
        char [] c = s.toCharArray();
        //创建HashMap用于统计频次
        HashMap<Character, Integer> map = new HashMap<>();
        //创建ArrayList用于保存顺序
        ArrayList<Character> list = new ArrayList<>();
        for(int i=0; i<c.length; i++){
            if (list.contains(c[i])){
                map.put(c[i], map.get(c[i])+1);
            }else {
                list.add(c[i]);
                map.put(c[i], 1);
            }
        }
        //遍历出现一次的字符就返回
        for (char val: list) {
            if(map.get(val)==1){
               return val;
            }
        }
        return ' ';
    }
}
